package com.feng.gaoerfu.holder;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.feng.gaoerfu.R;
import com.feng.gaoerfu.adapter.ViewPagerAdapter;
import com.feng.gaoerfu.base.App;
import com.feng.gaoerfu.bean.TableData;
import com.feng.gaoerfu.view.FrameLayoutCheckable;
import com.feng.gaoerfu.view.framework.picker.LinkagePicker;

import butterknife.ButterKnife;
import butterknife.InjectView;

import static com.feng.gaoerfu.base.App.mTableLists;

/**
 * @time 2017/8/9 0009 20:14
 */
public class HeaderViewHolder implements View.OnClickListener {
    private LinkagePicker mPicker;
    private int mColumn;
    private Context mContext;
    private int mRow;
    @InjectView(R.id.view_pager)
    ViewPager mViewPager;
    @InjectView(R.id.left_layout)
    LinearLayout mLeftLayout;
    @InjectView(R.id.right_layout)
    LinearLayout mRightLayout;
    @InjectView(R.id.layout_people0)
    FrameLayoutCheckable mLayoutPeople1;
    @InjectView(R.id.layout_people1)
    FrameLayoutCheckable mLayoutPeople2;
    @InjectView(R.id.layout_people2)
    FrameLayoutCheckable mLayoutPeople3;
    @InjectView(R.id.layout_people3)
    FrameLayoutCheckable mLayoutPeople4;
    @InjectView(R.id.content_person)
    LinearLayout mLayoutContent;
    private View mHeaderView;
    public static final String TAG = "HeaderViewHolder";


    public HeaderViewHolder(Context context, int mRow, int column, LinkagePicker picker) {
        this.mContext = context;
        this.mRow = mRow;
        this.mColumn = column;
        this.mPicker = picker;
        Log.d(TAG, "mRow = " + mRow + ",mColumn = " + mColumn);
        init();
    }


    private HeaderViewHolder init() {
        initView();
        initListener();
        return this;
    }

    private void initListener() {
        mLeftLayout.setOnClickListener(this);
        mRightLayout.setOnClickListener(this);
        mLayoutPeople1.setOnClickListener(this);
        mLayoutPeople2.setOnClickListener(this);
        mLayoutPeople3.setOnClickListener(this);
        mLayoutPeople4.setOnClickListener(this);


    }

    private void initView() {
        mHeaderView = View.inflate(mContext, R.layout.dialog_scoring_header, null);
        ButterKnife.inject(this, mHeaderView);
        mViewPager.setAdapter(new ViewPagerAdapter(App.mGolfHoleList));
        for (int i = App.mPersonList.size(); i < mLayoutContent.getChildCount(); i++) {
            mLayoutContent.getChildAt(i).setVisibility(View.INVISIBLE);
        }

        mViewPager.setCurrentItem(mColumn);
        refreshData();
    }

    private void refreshData() {
        //球员
        FrameLayoutCheckable childAt = (FrameLayoutCheckable) mLayoutContent.getChildAt(mRow);
        childAt.toggle();
        //球员数据
        for (int i = 0; i < App.mPersonList.size(); i++) {
            FrameLayoutCheckable layoutCheckable = (FrameLayoutCheckable) mLayoutContent.getChildAt(i);
            TextView mTvName = (TextView) layoutCheckable.getChildAt(0);
            TextView mTvNum = (TextView) layoutCheckable.getChildAt(1);
            mTvName.setText(App.mPersonList.get(i).name);
            mTvNum.setText(String.valueOf(mTableLists.get(i).get(getColumn()).numOfGan));
        }

        //杆数
        TableData tableData = App.mTableLists.get(mRow).get(getColumn());
        mPicker.setSelectedIndex(tableData.numOfGan == 0 ? tableData.defaullNum : tableData.numOfGan, tableData.numOfTui);
    }

    public View getHeaderView() {
        return mHeaderView;
    }

    @Override
    public void onClick(View v) {
        int currentItem = mViewPager.getCurrentItem();
        switch (v.getId()) {
            case R.id.left_layout:
                if (currentItem > 0) {
                    mColumn = currentItem - 1;
                    mViewPager.setCurrentItem(mColumn);
                    refreshData();
                }
                break;
            case R.id.right_layout:
                if (currentItem < App.mGolfHoleList.size() - 1) {
                    mColumn = currentItem + 1;
                    mViewPager.setCurrentItem(mColumn);
                    refreshData();
                }
                break;
            case R.id.layout_people0:
                mLayoutPeople1.toggle();
                mRow = 0;
                refreshData();
                break;
            case R.id.layout_people1:
                mLayoutPeople2.toggle();
                mRow = 1;
                refreshData();
                break;
            case R.id.layout_people2:
                mLayoutPeople3.toggle();
                mRow = 2;
                refreshData();
                break;
            case R.id.layout_people3:
                mLayoutPeople4.toggle();
                mRow = 3;
                refreshData();
                break;

        }
    }

    public int getRow() {
        return mRow;
    }

    public int getColumn() {
        return mViewPager.getCurrentItem();
    }
}
